﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BgSpawn : MonoBehaviour {

    [SerializeField]
    private float disDistance =2;

    private float LastPosY = 0f;
    [SerializeField]
    private List<Transform> Bgtransforms;

    void Start()
    {
        InitLastPostion();
    }

    void InitLastPostion()
    {
         foreach(var tm in Bgtransforms)
         {
             if (tm.localPosition.y < LastPosY)
                 LastPosY = tm.localPosition.y;
         }
    }

    public void OnTriggerEnter2D(Collider2D collision)
    {
        if(Mathf.Abs(collision.transform.localPosition.y - LastPosY) <0.1f)
        {
            for (var i = 0; i < Bgtransforms.Count; i++)
            {
                var b_tf = Bgtransforms[i];
                if (!b_tf.gameObject.activeSelf)
                {
                    b_tf.gameObject.SetActive(true);
                    b_tf.localPosition = new Vector3(0, LastPosY - disDistance, 0);
                    LastPosY -= disDistance;
                }
            }
        }
    }
}
